import {userinfo as testUserinfo} from '@/api/fakeData/userinfo'

const state = {
    isLogin: false,
    userInfo: {
        level: ''
    },
    requestSend: false
}

const getters = {
    userInfo: state => state.userInfo
}

const actions = {
    async getUserInfo ({state, commit}) {
        let userInfo
        if (process.env.NODE_ENV === 'production') {
            userInfo = JSON.parse(document.getElementById('userinfo').textContent)
        } else {
            userInfo = testUserinfo
        }
        commit('setUser', {userInfo})
        commit('requestSend')
        return userInfo
    }
}

const mutations = {
    setUser (state, {userInfo}) {
        state.userInfo = userInfo
    },
    changeUserLevel (state, level) {
        state.userInfo.level = level
    },
    requestSend (state) {
        state.requestSend = true
    }
}

export default {
    state,
    getters,
    actions,
    mutations,
    namespaced: true
}
